package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import oa.dao.AttendanceDao;
import oa.entity.Attendance;
import oa.exception.DaoException;
import oa.service.AttendanceService;
import oa.service.impl.AttendanceServiceImpl;

/**
 * Servlet implementation class UserStatistics
 */
@WebServlet("/userStatistics")
public class UserStatistics extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private int day;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserStatistics() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		String userId = request.getParameter("userId");
		String dates = request.getParameter("yM");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat dateType = new SimpleDateFormat("HH:mm");
		
		SimpleDateFormat tempDayType = new SimpleDateFormat("dd");
		
		AttendanceService attendanceService = new AttendanceServiceImpl();
		List<Attendance> start =new ArrayList<Attendance>();
		
		try {
			try {
				start = attendanceService.selectStartTime(Integer.parseInt(userId), dates, null);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (DaoException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			day = getDaysOfMonth(sdf.parse(dates));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		PrintWriter out = response.getWriter();
		String [] months = new String[day];
		for (int i = 0; i<months.length;i++) {
			months[i] = i+1+"";
		}
		String [] starttime = new String[day];
		String [] endtime =  new String[day];
		for (int i = 0; i<starttime.length;i++) {

			try {
				int tempDays = Integer.parseInt(tempDayType.format(start.get(i).getAttendanceStarttime()))-1;
				String tempStart = dateType.format(start.get(i).getAttendanceStarttime());
				starttime[tempDays] = tempStart.replace(":", ".");
				String tempEnd = dateType.format(start.get(i).getAttendanceEndtime());
				endtime[tempDays] = tempEnd.replace(":", ".");
				
			}catch (Exception e) {
				System.out.println("此异常无需处理");
			}
			if(starttime[i] == null) {
				starttime[i] = "无数据";
			}
			if(endtime[i] == null) {
				endtime[i] = "无数据";
			}
		}
		Map<String, Object> m = new HashMap<>();
		m.put("dates", months);
		m.put("sign", starttime);
		m.put("signout", endtime);
		out.print(JSON.toJSONString(m));
		out.flush();
		out.close();
		
	}
	/**
	 * 获取现在日期
	 * @param date
	 * @return
	 */
	public static int getDaysOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
